package com.example.crm.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.crm.entity.FollowUp;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 跟进记录Mapper接口
 */
@Mapper
public interface FollowUpMapper extends BaseMapper<FollowUp> {
    
    /**
     * 根据客户ID查询跟进记录
     */
    @Select("SELECT f.*, c.name as customer_name, ct.name as contact_name, o.name as opportunity_name " +
            "FROM crm_follow_up f " +
            "LEFT JOIN crm_customer c ON f.customer_id = c.id " +
            "LEFT JOIN crm_contact ct ON f.contact_id = ct.id " +
            "LEFT JOIN crm_opportunity o ON f.opportunity_id = o.id " +
            "WHERE f.customer_id = #{customerId} AND f.deleted = 0 " +
            "ORDER BY f.follow_up_time DESC")
    List<FollowUp> selectByCustomerId(@Param("customerId") Long customerId);
    
    /**
     * 根据销售机会ID查询跟进记录
     */
    @Select("SELECT f.*, c.name as customer_name, ct.name as contact_name, o.name as opportunity_name " +
            "FROM crm_follow_up f " +
            "LEFT JOIN crm_customer c ON f.customer_id = c.id " +
            "LEFT JOIN crm_contact ct ON f.contact_id = ct.id " +
            "LEFT JOIN crm_opportunity o ON f.opportunity_id = o.id " +
            "WHERE f.opportunity_id = #{opportunityId} AND f.deleted = 0 " +
            "ORDER BY f.follow_up_time DESC")
    List<FollowUp> selectByOpportunityId(@Param("opportunityId") Long opportunityId);
    
    /**
     * 获取最近的跟进记录
     */
    @Select("SELECT f.*, c.name as customer_name, ct.name as contact_name, o.name as opportunity_name " +
            "FROM crm_follow_up f " +
            "LEFT JOIN crm_customer c ON f.customer_id = c.id " +
            "LEFT JOIN crm_contact ct ON f.contact_id = ct.id " +
            "LEFT JOIN crm_opportunity o ON f.opportunity_id = o.id " +
            "WHERE f.deleted = 0 " +
            "ORDER BY f.follow_up_time DESC LIMIT #{limit}")
    List<FollowUp> selectRecentFollowUps(@Param("limit") Integer limit);
} 